import unittest
import TLCam
import InputController
import exchange_pb2
import util
import os
from google.protobuf.message import EncodeError
from google.protobuf.internal import encoder

class InputControllerTest(unittest.TestCase):
    
    # === InputController.requestController() =======================================
   
    def test_requestController_withoutTimestamp(self):
        ic = InputController.InputController()
        simRequest = exchange_pb2.Request()
        simRequest.type = simRequest.GET_MOVIE
         
        self.assertRaises(EncodeError, util.getDelimitMessage, simRequest)
   
    def test_requestController_getMovie_withoutParam(self):
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.GET_MOVIE)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue(response.type == response.STATUS, 'response type is not status')
        self.assertTrue("not available" in response.status, 'response status wrong')

    def test_requestController_getStatus(self):
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.GET_STATUS)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue("Not implemented" == response.status, 'response status wrong')

    def test_requestController_getSessions_withoutSessions(self):
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.GET_SESSIONS)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertEquals(len(response.sessions.session), 0, 'sessions list is greater than 0')
    
    def test_requestController_getSessions(self): 
        tlc = TLCam.getTLC()
        ic = InputController.InputController()
         
        tlc.createSession(1234567)
        
        simRequest = self.getRequestFor(exchange_pb2.Request.GET_SESSIONS)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertEquals(len(tlc.getSessions()), 1, 'response status wrong: ' + response.status)
    
        self.tidyUp()
    
    def test_requestController_setMovieTime_withoutParam(self): 
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.SET_MOVIE_TIME)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue("invalid" in response.status, 'response status wrong: ' + response.status)

    def test_requestController_setFps_withoutParam(self): 
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.SET_FPS)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue("invalid" in response.status, 'response status wrong: ' + response.status)

    def test_requestController_setTimePerImg(self): 
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.SET_TIME_PER_IMG)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue("invalid" in response.status, 'response status wrong: ' + response.status)

    def test_requestController_delImages(self): 
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.DEL_IMAGES)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue("Not implemented" == response.status, 'response status wrong')

    def test_requestController_delSession_withoutParam(self): 
        ic = InputController.InputController()
         
        simRequest = self.getRequestFor(exchange_pb2.Request.DEL_SESSION)
        response = self.getReponseObject(ic.requestController(simRequest))
        
        self.assertTrue("session is not available" == response.status, 'response status wrong: ' + response.status)
    
    def test_requestController_delSession(self): 
        tlc = TLCam.getTLC()
        ic = InputController.InputController()
         
        tlc.createSession(1234567)
        
        simRequest = exchange_pb2.Request()
        simRequest.type = simRequest.DEL_SESSION
        simRequest.timestamp = str(123567)
        simRequest.param = 'tlc_1001'

        response = self.getReponseObject(ic.requestController(simRequest.SerializeToString()))
        
        self.assertEquals(len(tlc.getSessions()), 0, 'response status wrong: ' + response.status)
        self.tidyUp()
    
    # --- helper -------
    def getRequestFor(self, type):
        simRequest = exchange_pb2.Request()
        simRequest.type = type
        simRequest.timestamp = str(123567)
        return simRequest.SerializeToString()
    
    def getReponseObject(self, res):
        response = exchange_pb2.Response()
        response.ParseFromString(res.SerializeToString())
        return response
    
    def tidyUp(self): 
        try: 
            os.remove('./tlc_1001/meta.tlc')
            os.rmdir('./tlc_1001')
        except:
            pass

# ==============================================    
if __name__ == "__main__": 
    unittest.main()